package top.someapp.applib;

/**
 * @author zwz
 * Created on 2020-03-30
 */
public enum AndroidVersion {
    /**
     * October 2008: The original, first, version of Android. Yay!
     */
    BASE(1, "1"),

    /**
     * February 2009: First Android update, officially called 1.1.
     */
    BASE_1_1(2, "1.1"),

    /**
     * May 2009: Android 1.5.
     */
    CUPCAKE(3, "1.5"),

    /**
     * September 2009: Android 1.6.
     */
    DONUT(4, "1.6"),

    /**
     * November 2009: Android 2.0
     */
    ECLAIR(5, "2.0"),

    /**
     * December 2009: Android 2.0.1
     */
    ECLAIR_0_1(6, "2.0.1"),

    /**
     * January 2010: Android 2.1
     */
    ECLAIR_MR1(7, "2.1"),

    /**
     * June 2010: Android 2.2
     */
    FROYO(8, "2.2"),

    /**
     * November 2010: Android 2.3
     */
    GINGERBREAD(9, "2.3"),

    /**
     * February 2011: Android 2.3.3.
     */
    GINGERBREAD_MR1(10, "2.3.3"),

    /**
     * February 2011: Android 3.0.
     */
    HONEYCOMB(11, "3.0"),

    /**
     * May 2011: Android 3.1.
     */
    HONEYCOMB_MR1(12, "3.0"),

    /**
     * June 2011: Android 3.2.
     */
    HONEYCOMB_MR2(13, "3.2"),

    /**
     * October 2011: Android 4.0.
     */
    ICE_CREAM_SANDWICH(14, "4.0"),

    /**
     * December 2011: Android 4.0.3.
     */
    ICE_CREAM_SANDWICH_MR1(15, "4.0.3"),

    /**
     * June 2012: Android 4.1.
     */
    JELLY_BEAN(16, "4.1"),

    /**
     * November 2012: Android 4.2, Moar jelly beans!
     */
    JELLY_BEAN_MR1(17, "4.2"),

    /**
     * July 2013: Android 4.3, the revenge of the beans.
     */
    JELLY_BEAN_MR2(18, "4.3"),

    /**
     * October 2013: Android 4.4, KitKat, another tasty treat.
     */
    KITKAT(19, "4.4"),

    /**
     * June 2014: Android 4.4W. KitKat for watches, snacks on the run.
     */
    KITKAT_WATCH(20, "4.4W"),

    /**
     * Temporary until we completely switch to {@link #LOLLIPOP}.
     */
    L(21, "5.0"), // guess it's called 5.0

    /**
     * November 2014: Lollipop. A flat one with beautiful shadows. But still tasty.
     */
    LOLLIPOP(21, "5.0"),

    /**
     * March 2015: Lollipop with an extra sugar coating on the outside!
     */
    LOLLIPOP_MR1(22, "5.1"),

    /**
     * M is for Marshmallow!
     */
    M(23, "6.0"),

    /**
     * N is for Nougat.
     */
    N(24, "7.0"),

    /**
     * N MR1: Nougat++.
     */
    N_MR1(25, "7.1"),

    /**
     * O.
     */
    O(26, "8.0"),

    /**
     * O MR1.
     */
    O_MR1(27, "8.1"),

    /**
     * P.
     */
    P(28, "9.0"),

    /**
     * Q.
     * <p>
     * <em>Why? Why, to give you a taste of your future, a preview of things
     * to come. Con permiso, Capitan. The hall is rented, the orchestra
     * engaged. It's now time to see if you can dance.</em>
     */
    Q(29, "10.0");

    public final int code;
    public final String versionName;
    private int _major = -1;
    private int _minor = -1;
    private int _patch = -1;

    AndroidVersion(int code, String name) {
        this.code = code;
        this.versionName = name;
    }

    private int versionPart(int index) {
        String[] parts = versionName.split("[.]");
        if (index + 1 > parts.length) {
            return 0;
        }
        String part = parts[index];
        int value = 0;
        for (char ch : part.toCharArray()) {
            if (ch >= '0' && ch <= '9') {
                value = value * 10 + (ch - '0');
            } else {
                break;
            }
        }
        return value;
    }

    public int major() {
        if (_major == -1) {
            _minor = versionPart(0);
        }
        return _major;
    }

    public int minor() {
        if (_minor == -1) {
            _minor = versionPart(1);
        }
        return _minor;
    }

    public int patch() {
        if (_patch == -1) {
            _patch = versionPart(2);
        }
        return _patch;
    }
}
